package com.yswl.yeeduo.ui.adapter;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.ui.bean.BannerBean;
import com.yswl.yeeduo.ui.utils.ImageUtil;

/**
 * 楼盘 主页banner
 * 
 * @author Administrator
 */

public class BannerViewPagerAdapter extends PagerAdapter {

	public interface OnClickPagerListener {
		public void onClikPager(View v);
	};

	private OnClickPagerListener mClickListener;
	private List<View> mData = new ArrayList<View>();
	private ArrayList<BannerBean> dataList = new ArrayList<BannerBean>();
	
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions mOptions =  ImageUtil.getDefaultOptions() ;
	
	public BannerViewPagerAdapter(final Context context, ArrayList<BannerBean> dataList) {
		super();
		this.dataList = dataList;
		String imageUri = "drawable://" + R.drawable.aio_image_default_round; //本地图片路径
		LayoutInflater inflater = LayoutInflater.from(context);
		// 默认图片
		for (int i = 0; i < dataList.size(); i++) {
			View view = inflater.inflate(R.layout.viewpager_item, null);
			ImageView image = (ImageView) view.findViewById(R.id.image);
//			image.setImageResource(R.drawable.aio_image_default_round);
			imageLoader.displayImage(imageUri, image);
			mData.add(view);
		}
	}

	
	
	@Override
	public int getCount() {
		if (dataList.size() <= 2) {
			return dataList.size();
		}
		return Integer.MAX_VALUE;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView(mData.get(position % dataList.size()));
		container.removeView((View)object);
		// position % urls.length 取余数
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {

		int num = container.getChildCount();
		for (int i = 0; i < num; i++) {
			if (container.getChildAt(i).hashCode() == mData.get(position % dataList.size()).hashCode()) {
				container.removeView(mData.get(position % dataList.size()));
				break;
			}
		}
		View view = mData.get(position % dataList.size());
		ImageView imageView = (ImageView) view.findViewById(R.id.image);
		imageLoader.displayImage(dataList.get(position % dataList.size()).url, imageView,mOptions);
//		view.setTag(position % dataList.size());
//		view.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				mClickListener.onClikPager(v);
//			}
//		});

		container.addView(view);
		
		return view;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	public void setAdaperOnClickListener(OnClickPagerListener listener) {
		this.mClickListener = listener;
	}
	
//	@Override  
//    public int getItemPosition(Object object) {  
//     return POSITION_NONE;  
//    }  
}